package com.young.cms.service.impl;

import com.young.cms.util.CmsConstants;
import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.cms.model.AdministrativeRegion;
import com.young.cms.service.IAdministrativeRegionService;
import com.young.common.util.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 行政区划服务实现
* Created by imrookie on 2018-10-16.
*/
@Service("administrativeRegionService")
public class AdministrativeRegionServiceImpl implements IAdministrativeRegionService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(AdministrativeRegionServiceImpl.class);

    private static final String MAPPER = "com.young.cms.mapper.administrativeRegion";//mapper的namespace

    //获取数据列表
    @Override
    public List<AdministrativeRegion> getList(AdministrativeRegion obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<AdministrativeRegion> getPage(AdministrativeRegion obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    /**
     * 查询省份列表
     *
     * @return
     */
    @Override
    public List<AdministrativeRegion> getProvinceList() {
        AdministrativeRegion obj = new AdministrativeRegion();
        obj.setAdminLevel(CmsConstants.AdministrativeRegionLevel.PROVINCE);
        return this.getList(obj);
    }

    /**
     * 根据省份编码查询市列表
     *
     * @param provinceCode 省份编码
     * @return
     */
    @Override
    public List<AdministrativeRegion> getCityListByProvinceCode(String provinceCode) {
        if (StringUtils.isBlank(provinceCode)){
            throw new BaseException("省份编码无效");
        }
        AdministrativeRegion obj = new AdministrativeRegion();
        obj.setAdminLevel(CmsConstants.AdministrativeRegionLevel.CITY);
        obj.setParentId(provinceCode);
        return this.getList(obj);
    }

    /**
     * 根据市编码查询县列表
     *
     * @param cityCode 市编码
     * @return
     */
    @Override
    public List<AdministrativeRegion> getCountyListByCityCode(String cityCode) {
        if (StringUtils.isBlank(cityCode)){
            throw new BaseException("省份编码无效");
        }
        AdministrativeRegion obj = new AdministrativeRegion();
        obj.setAdminLevel(CmsConstants.AdministrativeRegionLevel.COUNTY);
        obj.setParentId(cityCode);
        return this.getList(obj);
    }

    //查询单条
    @Override
    public AdministrativeRegion get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(AdministrativeRegion obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(AdministrativeRegion obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(AdministrativeRegion obj) {
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<AdministrativeRegion> list) {
        int i = 0;
        for(AdministrativeRegion item : list){
            i += this.insert(item);
        }
        return i;
    }

}